package UserLogin;

import World.Application;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LogInController {

    private int width;
    private int height;
    private Application application;
    private LogInPanel logInPanel;
    private String user;
    private char[] password;

    public LogInController(int width, int height, Application application) {
        this.width = width;
        this.height = height;
        this.application = application;
    }

    public void execute() {
        logInPanel = new LogInPanel(width, height);
        logInPanel.getOkButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                destroyUserPanel();
            }
        });
        logInPanel.getCancelButton().addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                destroyUserPanel();
            }
        });
        application.getPrincipalPanel().setVisible(false);
        application.add(logInPanel);
    }

    private void destroyUserPanel() {
        user = logInPanel.getUser().getText();
        password = logInPanel.getPassword().getPassword();
        application.remove(logInPanel);
        application.getPrincipalPanel().setVisible(true);
        application.destroyUserLogin();
    }

    public String getUser() {
        return user;
    }
}
